package com.bjpowernode.boss.mvc.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bjpowernode.boss.common.constant.SysConstans;
import com.bjpowernode.boss.common.enums.ResponseCode;
import com.bjpowernode.boss.convert.PaymentConvert;
import com.bjpowernode.boss.dao.entity.PayWay;
import com.bjpowernode.boss.dao.result.DicEntity;
import com.bjpowernode.boss.mvc.models.AjaxResult;
import com.bjpowernode.boss.mvc.models.pages.PageInfo;
import com.bjpowernode.boss.mvc.service.PayWayService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yu
 * @date 2024/7/12  22:22
 */

@CrossOrigin
@RequestMapping("/payment/way")
@RestController
public class PayWayController extends BaseController {

    @Resource
    private PaymentConvert paymentConvert ;

    @Resource
    private PayWayService payWayService ;

    @GetMapping("/pageList")
    public AjaxResult pageList(@RequestParam("pageNo") Integer pageNo, @RequestParam("productCode") String productCode ) {
        // 1.合法化pageNo
        pageNo = defPageNo(pageNo) ;

        // 2.分页查询
        IPage<PayWay> pageResult = payWayService.pageList(pageNo, SysConstans.PAGE_SIZE, productCode) ;

        // 3.获得分页参数
        // 总页数
        long totalPage = pageResult.getPages() ;
        // 分页查询的记录（list）
        List<PayWay> payWayList = pageResult.getRecords() ;

        // 4.创建分页对象
        PageInfo pageInfo = new PageInfo(pageResult.getCurrent(), totalPage) ;

        // 5.包装发给前端的数据
        Map<String, Object> data = new HashMap<>() ;
        data.put("list", paymentConvert.toListPayWayVo(payWayList)) ;
        data.put("pageInfo", pageInfo) ;

        return AjaxResult.OK(data) ;
    }

    /**
     * 支付方式字典接口
     * @return
     */
    @GetMapping("/dicList")
    public AjaxResult payWayDicList() {
        List<DicEntity> payWayDicList = payWayService.getPayWayDicListFromCache() ;
        return AjaxResult.OK(payWayDicList) ;
    }

    /**
     * 支付类型的字典接口
     * @param wayCode
     * @return
     */
    @GetMapping("/type/dicList")
    public AjaxResult payWayTypeDicList(@RequestParam String wayCode) {
        if (StrUtil.isNotBlank(wayCode)) {
            List<DicEntity> payWayTypeDicList = payWayService.getPayWayTypeDicListFromCache(wayCode.trim().toUpperCase()) ;
            return AjaxResult.OK(payWayTypeDicList) ;
        }
        else return AjaxResult.FAIL(ResponseCode.REQUEST_PARAM_ERR) ;
    }
}
